<?php

declare(strict_types = 1);
namespace app\api\controller;


use app\api\business\User as UserBus;
use app\api\validate\User as UserValidate;
use app\BaseController;

class Login extends BaseController
{

    public function index() :object{
        if (!$this->request->isPost()){
            return show(config('status.error'), '非法请求');
        }
        $phoneNumber = $this->request->param("phone_number", "", "trim");
        $code = input("code", "", "trim");
        $type = input("type", "", "trim");
        $data = [
            'phone_number' => $phoneNumber,
            'code' => $code,
            'type' => $type
        ];
        $validate = new UserValidate();
        if (!$validate->scene('login')->check($data)){
            return show(config('status.error'), $validate->getError());
        }
        try {
            $result = (new UserBus())->login($data);
        }catch (\Exception $e){
            return show($e->getCode(),$e->getMessage());
        }
        if ($result){
            return show(config('status.success'), '登录成功',$result);
        }
            return show(config('status.error'), '登录失败');

    }
}